pragma circom 2.0.0;

// Credit verification circuit
// This circuit verifies that a user's credit score is above a threshold
// without revealing the actual credit score

template CreditVerification() {
    // Private inputs
    signal private input creditScore;
    signal private input threshold;

    // Public inputs
    signal output isValid;

    // Verify that credit score is greater than or equal to threshold
    component isGreaterThan = GreaterThan();
    isGreaterThan.in[0] <== creditScore;
    isGreaterThan.in[1] <== threshold;

    // Output the result
    isValid <== isGreaterThan.out;
}

// Helper template to check if one number is greater than another
template GreaterThan() {
    signal input in[2];
    signal output out;

    // Convert inputs to binary
    component n2b1 = Num2Bits(32);
    component n2b2 = Num2Bits(32);
    n2b1.in <== in[0];
    n2b2.in <== in[1];

    // Compare bits
    component isGreater = IsGreater();
    isGreater.in[0] <== in[0];
    isGreater.in[1] <== in[1];

    out <== isGreater.out;
}

// Template to convert a number to binary
template Num2Bits(n) {
    signal input in;
    signal output out[n];
    var lc1 = 0;

    for (var i = 0; i < n; i++) {
        out[i] <-- (in >> i) & 1;
        out[i] * (out[i] - 1) === 0;
        lc1 += out[i] * (1 << i);
    }

    lc1 === in;
}

// Template to check if one number is greater than another
template IsGreater() {
    signal input in[2];
    signal output out;

    // Check if in[0] > in[1]
    component diff = LessThan(32);
    diff.in[0] <== in[1];
    diff.in[1] <== in[0];
    out <== diff.out;
}

// Template to check if one number is less than another
template LessThan(n) {
    signal input in[2];
    signal output out;

    // Convert inputs to binary
    component n2b1 = Num2Bits(n);
    component n2b2 = Num2Bits(n);
    n2b1.in <== in[0];
    n2b2.in <== in[1];

    // Compare bits
    component isLess = IsLess(n);
    isLess.in[0] <== in[0];
    isLess.in[1] <== in[1];

    out <== isLess.out;
}

// Template to check if one number is less than another
template IsLess(n) {
    signal input in[2];
    signal output out;

    // Convert inputs to binary
    component n2b1 = Num2Bits(n);
    component n2b2 = Num2Bits(n);
    n2b1.in <== in[0];
    n2b2.in <== in[1];

    // Compare bits
    component isLess = IsLessBits(n);
    isLess.in1 <== n2b1.out;
    isLess.in2 <== n2b2.out;

    out <== isLess.out;
}

// Template to check if one binary number is less than another
template IsLessBits(n) {
    signal input in1[n];
    signal input in2[n];
    signal output out;

    // Compare bits from most significant to least significant
    component isLess = IsLessBit();
    isLess.in1 <== in1[n-1];
    isLess.in2 <== in2[n-1];

    out <== isLess.out;
}

// Template to check if one bit is less than another
template IsLessBit() {
    signal input in1;
    signal input in2;
    signal output out;

    // Check if in1 < in2
    out <== in2 - in1;
}

component main = CreditVerification(); 